package com.msun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.msun.constant.RedisConstant;
import com.msun.dao.PlanDao;
import com.msun.dao.SetmealDao;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.pojo.CheckGroup;
import com.msun.pojo.Plan;
import com.msun.pojo.Setmeal;
import com.msun.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 体检套餐服务
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService{
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private PlanDao planDao;

    //新增套餐信息，同时需要关联检查组
    @Override
    public void edit(Map<String,Object> map) {
        Object checkGroupIdsOBJ = map.get("checkGroupIds");
        Object setmealOBJ =  map.get("setmeal");

        String checkGroupIdsSTR = JSONObject.toJSONString(checkGroupIdsOBJ);
        List<Integer> checkGroupIds = JSONObject.parseObject(checkGroupIdsSTR, List.class);


        String setmealSTR = JSONObject.toJSONString(setmealOBJ);
        Setmeal setmeal = JSONObject.parseObject(setmealSTR, Setmeal.class);


        // 先保存主体
        setmealDao.edit(setmeal);

        Integer setmealId = setmeal.getId();
        setmealDao.deleteRefer(setmealId);
        // 重新添加关系
        for (Integer itemId : checkGroupIds) {
            setmealDao.addRefer(setmealId,itemId);
        }


    }

    @Override
    public void delete(Integer id) {
        int i = setmealDao.setmeal4order(id);
        if (i!=0){
            // 套餐被订单关联了,不能删除
            throw new RuntimeException();
        }
        // 先删套餐关联的外键
        setmealDao.deleteRefer(id);
        // 再删主体
        setmealDao.delete(id);

    }

    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        // 添加套餐信息
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckgroup(setmealId,checkgroupIds);
        //将图片名称保存到Redis集合中
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    //根据套餐ID查询套餐详情（套餐基本信息、套餐对应的检查组信息、检查组对应的检查项信息）
    public Setmeal findById(int id) {
        return setmealDao.findById(id);
    }

    //查询套餐预约占比数据
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

    @Override
    public Setmeal findSetmealById(Integer id) {
        Setmeal setmeal=setmealDao.findSetmealById(id);

        return setmeal;
    }

    @Override
    public void save(Map<String, Object> map) {
        Object memberid = map.get("memberid");
        if (memberid==null){
            return;
        }

        Integer m = (Integer) memberid;

        // 查询详细的套餐
        Plan plan  = new Plan();
        // 保存体检结果
        plan.setResult("正常");
        planDao.save(plan);

    }

    @Override
    public Setmeal setmealInfoById(Integer setMealId) {
       return setmealDao.setmealInfoById(setMealId);
    }
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        // 执行分页查询获取到套餐
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findPage(queryString);
        // 获取套餐对应的检查组
        for (Setmeal setmeal : page) {
            List<CheckGroup> itemIdByGroup = setmealDao.findCheckGroupBySetmeal(setmeal.getId());
            setmeal.setCheckGroups(itemIdByGroup);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }
    //设置套餐和检查组多对多关系，操作t_setmeal_checkgroup
    public void setSetmealAndCheckgroup(Integer setmealId,Integer[] checkgroupIds){
        if(checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
}
